/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "../lib/core.h"
#include "../lib/io.h"
#include "../lib/algorithm.h"

using namespace gctl;

int main(int argc, char const *argv[]) try
{
	int fs = 200;
    double f1 = 4.0;
    double f2 = 10.0;
    int m = 600;

    // Create filters
    fir_filter filter_lp(LowPass, Hamming, m, (double) fs, f1);
    fir_filter filter_hp(HighPass, Hamming, m, (double) fs, f2);
    fir_filter filter_bp(BandPass, Hamming, m, (double) fs, f1, f2);
    fir_filter filter_sb(BandStop, Hamming, m, (double) fs, f1, f2);

    // Get coefficient
    //array<double> h_lp = filter_lp.get_coefficients();
    //array<double> h_hp = filter_hp.get_coefficients();
    //array<double> h_bp = filter_bp.get_coefficients();
    //array<double> h_sb = filter_sb.get_coefficients();
    
    // Demo
    double f1_demo = 1.0; // Hz
    double f2_demo = 7.5;
    double f3_demo = 15.0;

    // Create input signal
    gctl::array<double> t, signal(10.13*fs);
    gctl::linespace(0.0, 10.13, 10.13*fs, t);

    for (size_t i = 0; i < t.size(); i++)
    {
        signal[i] = 2.0*sin(2.0*GCTL_Pi*f1_demo*t[i]) + sin(2.0*GCTL_Pi*f2_demo*t[i]) + 0.5*sin(2.0*GCTL_Pi*f3_demo*t[i]);
    }

    gctl::array<double> filtered_lp, filtered_hp, filtered_bp, filtered_sb;

    // Run the filters
    filter_lp.filter(signal, filtered_lp);
    filter_hp.filter(signal, filtered_hp);
    filter_bp.filter(signal, filtered_bp);
    filter_sb.filter(signal, filtered_sb);

    std::ofstream ofile;
    open_outfile(ofile, "fir_filter_ex.out");
    for (size_t i = 0; i < filtered_lp.size(); i++)
    {
        ofile << t[i] << " " << signal[i] << " " << filtered_lp[i] << " " << filtered_hp[i] << " " << filtered_bp[i] << " " << filtered_sb[i] << "\n";
    }
    ofile.close();
    return 0;
}
catch(std::exception &e)
{
    GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}